using System.Collections.Generic;
using System.Linq;
using TEngine;
using UnityEngine;
using UnityEngine.UI;

namespace GameLogic
{
    /// <summary>
    /// 数据
    /// </summary>
    class m_BagItemListData
    {
        public List<m_BagData> m_BagDatas = new List<m_BagData>();
    }

    class m_BagData
    {
        public int id;
        public int amount;
    }


    [Window(UILayer.UI)]
    class UI_BagListView : UIWindow
    {
        #region 脚本工具生成的代码
        private ScrollRect _scrollView;
        private GameObject _itemList;
        protected override void ScriptGenerator()
        {
            _scrollView = FindChildComponent<ScrollRect>("m_scrollView");
            _itemList = FindChild("m_scrollView/Viewport/Content/SlotBagRow").gameObject;
        }
        #endregion

        #region 事件
        #endregion
        protected UILoopListWidget<m_BagItemList, m_BagItemListData> m_loopList;
        protected override void OnCreate()
        {
            base.OnCreate();
            //创建循环列表的item和相应数据
            m_loopList = CreateWidget<UILoopListWidget<m_BagItemList, m_BagItemListData>>(_scrollView.gameObject);
            //给循环列表赋值item
            m_loopList.itemBase = _itemList;

            // 获取玩家的真实背包数据
            Dictionary<int, InventoryItem> actualBagItems = UserInfo.Instance.GetUserBagInfo();
            int itemsPerList = 4;

            List<m_BagItemListData> datas = new List<m_BagItemListData>();

            // 将 Dictionary 的值转换为 List 进行处理
            List<InventoryItem> bagItemList = actualBagItems.Values.ToList();

            for (int i = 0; i < bagItemList.Count; i += itemsPerList)
            {
                m_BagItemListData dataItem = new m_BagItemListData();

                int numberOfItems = (i + itemsPerList > bagItemList.Count) ? bagItemList.Count - i : itemsPerList;
                for (int j = 0; j < numberOfItems; j++)
                {
                    m_BagData bagData = new m_BagData();
                    bagData.id = bagItemList[i + j].itemID; // 使用实际的 itemID
                    bagData.amount = bagItemList[i + j].itemAmount;
                    dataItem.m_BagDatas.Add(bagData);
                }

                datas.Add(dataItem);
            }

            m_loopList.SetDatas(datas);

            Debug.Log(m_loopList.GetItemByIndex(0).transform.position.y);
        }
    }
}

